<?php
class M_student extends CI_Model{
	
	function add_student ($v){
			
		$user_data = array(
			'username' => $v->username,
			'password' => $this->hash_password($v->password),
			'usertype' => 'student',
			'created' => NOW
		);
				
		$this->db->insert('users', $user_data);
		if($this->db->affected_rows() > 0){
			$student_data = array(
				'id_no' => $v->id_no,
				'user_id' => $this->db->insert_id(),
				'course_id' => $v->course_code,
				'year_level_id' => $v->year_level_id,
				'lastname' => $v->lastname,
				'firstname' => $v->firstname,
				'middlename' => $v->middlename,
				'gender' => $v->gender,
				'civilstatus' => $v->civilstatus,
				'dob' => $v->dob,
				'pob' => $v->pob,
				'age' => $v->age,
				'disability' => $v->disability,
				'nationality' => $v->nationality,
				'religion' => $v->religion,
				'contactnumber' => $v->contactnumber,
				'email' => $v->email,
				'presentaddress' => $v->presentaddress,
				'fathername' => $v->fathername,
				'fatheroccupation' => $v->fatheroccupation,
				'fathercontact' => $v->fathercontact,
				'mothername' => $v->mothername,
				'motheroccupation' => $v->motheroccupation,
				'mothercontact' => $v->mothercontact,
				'parentaddress' => $v->parentaddress,
				'guardianname' => $v->guardianname,
				'guardiancontact' => $v->guardiancontact,
				'guardianaddress' => $v->guardianaddress,
				'eschoolname' => $v->eschoolname,
				'eschooladdress' => $v->eschooladdress,
				'eyeargraduate' => $v->eyeargraduate,
				'sschoolname' => $v->sschoolname,
				'sschooladdress' => $v->sschooladdress,
				'syeargraduate' => $v->syeargraduate,
				'vschoolname' => $v->vschoolname,
				'vschooladdress' => $v->vschooladdress,
				'vyeargraduate' => $v->vyeargraduate,
				'tschoolname' => $v->tschoolname,
				'tschooladdress' => $v->tschooladdress,
				'tyeargraduate' => $v->tyeargraduate,
				'oschoolname' => $v->oschoolname,
				'oschooladdress' => $v->oschooladdress,
				'oyeargraduate' => $v->oyeargraduate,
				'created' => NOW
			);
			$this->db->insert('student_profiles', $student_data);
			return $this->db->affected_rows() > 0 ? TRUE : FALSE;
		}
		else {
			return false;	
		}
		
	
	}
	
	function get_pending_enrollees(){
			
	}
	
	function get_profile($user_id){
		$this->db->where('user_id', $user_id);
		$query = $this->db->get('student_profiles');
		return $query->result();		
	}	
	
	public function hash_password($password)
    {
        $salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
        $hash = hash('sha256', $salt . $password);

        return $salt . $hash;
    }
	
	
	public function update_stud_profile($data,$user_id){
		$this->db->set($data)->where('user_id',$user_id)->update('student_profiles');
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function enroll_student($id_no){
		$this->db->set(array('enrollmentstatus' => 'pending'))->where('id_no', $id_no)->update('student_profiles');
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function get_all_pending_enrollees(){
		$q = 'SELECT student_profiles.id_no, firstname, lastname, middlename, course_code, t.enrolled_subjects FROM student_profiles 
LEFT JOIN courses ON student_profiles.course_id = courses.course_id
LEFT JOIN (SELECT id_no, count(*) as enrolled_subjects FROM student_subjects GROUP BY id_no) as t ON t.id_no = student_profiles.id_no
WHERE enrollmentstatus = "pending"';
		$query = $this->db->query($q);
		
		return $query->num_rows() > 0 ? $query->result() : FALSE;
	}
	
	public function get_enrollee($id_no){
		$q = 'SELECT student_profiles.id_no, firstname, lastname, middlename, course_code, level_name FROM student_profiles 
LEFT JOIN courses ON student_profiles.course_id = courses.course_id LEFT JOIN year_level ON year_level.id = student_profiles.year_level_id
WHERE id_no = "'.$id_no.'"';
		$query = $this->db->query($q);
		
		
		return $query->num_rows() > 0 ? $query->row() : FALSE;
	}
	
	public function verify_student($id_no){
		$this->db->set(array('enrollmentstatus' => 'approved'))->where('id_no', $id_no)->update('student_profiles');
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function get_student_status($id_no){
		$query = $this->db->select('enrollmentstatus')->from('student_profiles')->where('id_no', $id_no)->get();	
		return $query->num_rows() > 0 ? $query->row() : FALSE;
	}
}
?>